home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / RCVBLK.C < prev    next >
Text File  |  1985-04-01  |  4KB  |  112 lines

  1. /*
  2.  *  receive blocks of data
  3.  */
  4. #define BLKLENTH 128
  5. #define SEQLENTH   2
  6. #define CHKSMLEN   2
  7. #define NOT_END  endfound==0
  8. #define LAST_BLK shortblk==1
  9. #define ENDOFBLK '+'
  10. rcvblk()
  11.     {
  12.     /*
  13.      * Declarations
  14.      */
  15.     short rcvcntr,                            /*                              */
  16.           inpcntr,                            /* No of bytes in this block    */
  17.           lastblck,                           /* Last block's sequence no     */
  18.           blockno,                            /* Current block no             */
  19.           shortblk,                           /* Short block flag             */
  20.           sequno[SEQLENTH+1],                 /* Current sequence no array    */
  21.           block[BLKLENTH+1],                  /* Data array                   */
  22.           checksum[CHKSMLEN+1],               /* Checksum array               */
  23.           compchk,                            /* Computed checksum var        */
  24.           com1,                               /* Input file descriptor var    */
  25.           endfound,                           /* End of transmission flag     */
  26.           cntr;                               /* General purpose counter      */
  27.     /*
  28.      * Initialise
  29.      */
  30.     rcvcntr = 0;                              /* No of blocks received        */
  31.     endfound = 0;                             /* End not yet found            */
  32.     lastblck = 0;                             /* Next block no should be 1    */
  33.     sequno[SEQLENTH+1] = '\0';                /* Initialise to valid string   */
  34.     block[BLKLENTH+1] = '\0';                 /* Initialise to valid string   */
  35.     checksum[CHKSMLEN+1] = '\0';              /* Initialise to valid string   */
  36.     com1 = fopen("a:com1.dat","r");           /* Open COM1 for input          */
  37.     /*
  38.      * Begin processing
  39.      */
  40.     while (NOT_END)
  41.         {
  42.         /*
  43.          * Read the sequence number
  44.          */
  45.         for (cntr = 1, cntr <= SEQLENTH, ++cntr)
  46.             {
  47.             seqno[cntr] = fgetc(com1);
  48.             if (seqno[cntr] == EOF)           /* Check for error or EOF       */
  49.                 endfound = 1;
  50.             }
  51.         /*
  52.          * Check for ET (End of Transmission)
  53.          */
  54.         if (NOT_END)
  55.             {
  56.             if (seqno[] == "ET")
  57.                 {
  58.                 endfound = 1;
  59.                 }
  60.             }
  61.         /*
  62.          * Convert the sequence chars to a valid no
  63.          */
  64.         if (NOT_END)
  65.             {
  66.             if (isdigit(seqno[1]) && isdigit(seqno[2]))
  67.                 {
  68.                 blockno = atoi(seqno[]); /* This block's no                   */
  69.                 if (blockno != lastblck+1)
  70.                     endfound = 1;
  71.                 else
  72.                     ++lastblck;
  73.                 }
  74.             else
  75.                 endfound = 1;
  76.             }
  77.         /*
  78.          * Receive 128 chars and compute checksum
  79.          */
  80.         if (NOT_END)
  81.             {
  82.             ++rvccntr;
  83.             compchk = 0;
  84.             for (inpcntr=1, inpcntr <= BLKLENTH, ++inpcntr)
  85.                 {
  86.                 block[inpcntr] = fgetc(com1);
  87.                 if (block[inpcntr] = ENDOFBLK)
  88.                     {
  89.                     shortblk = 1;
  90.                     inpcntr = BLKLENTH + 1;
  91.                     }
  92.                 else
  93.                     compchk = ((compchk + block[inpcntr])/100);
  94.                 }
  95.             }
  96.         /*
  97.          * Read checksum
  98.          */
  99.         if (NOT_END)
  100.             {
  101.             checksum = 0;
  102.             for (cntr = 1, cntr <= CHKSMLEN, ++cntr)
  103.                 checksum[cntr] = fgetc(com1);
  104.             /*
  105.              * Compare checksum
  106.              */
  107.             if (compchk != atoi(checksum[])
  108.                 endfound = 1;
  109.             }
  110.         }
  111.     }
  112.